home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / comm / tcp / rxsocket.lha / rxsocket / examples / udp.rexx < prev   
Encoding:
OS/2 REXX Batch file  |  1998-10-27  |  1.9 KB  |  106 lines

  1. /*
  2.     udp test translated into rexx
  3.     from Amiga Magazine n.93 udp.c test by Rudi Chiarito
  4.  
  5.     Show the use of udp protocol.
  6.     Usage: udp <host> <port> <localPort>
  7.  
  8.     i.e.:
  9.         shell1 udp localhost 4000 4001
  10.         shell2 udp localhost 4001 4000
  11.  
  12.     to stop push <ctrl-c> in the shells.
  13.  
  14.     To see how rx/tx is stopped just write something in one or both
  15.     the shells.
  16.  
  17. */
  18.  
  19. parse arg host remotePort localPort .
  20.  
  21. /**START libraries*/
  22. if ~show("L","rexxsupport.library") then
  23.     if ~addlib("rexxsupport.library",0,-30) then do
  24.         say "no rexxsupport.library"
  25.         exit
  26.     end
  27. if ~show("L","rxsocket.library") then
  28.     if ~addlib("rxsocket.library",0,-30) then do
  29.         say "no rxsocket.library"
  30.         exit
  31.     end
  32. if ~show("L","rmh.library") then
  33.     if ~addlib("rmh.library",0,-30) then do
  34.         say "no rmh.library"
  35.         exit
  36.     end
  37. /**END libraries**/
  38.  
  39. if IsDotAddr(host) then do
  40.     remote.addrFamily = "INET"
  41.     remote.addrAddr   = host
  42. end
  43. else do
  44.     if ~gethostbyname("H",host) then do
  45.         say "udp: host" host "not found."
  46.         exit
  47.     end
  48.     remote.addrFamily = h.hostAddrType
  49.     remote.addrAddr   = h.hostAddrList.0
  50. end
  51.  
  52. remote.addrPort = remotePort
  53.  
  54. sock = socket("INET","DGRAM","IP")
  55. if sock<0 then do
  56.     say "udp: can't create socket:" errno()
  57.     exit
  58. end
  59.  
  60. local.addrFamily = "INET"
  61. local.addrAddr     = 0
  62. local.addrPort   = localPort
  63. if bind(sock,"LOCAL")<0 then do
  64.     say "udp: can't bind port:" errno()
  65.     exit
  66. end
  67.  
  68. call IOCtlSocket(sock,"FIONBIO",1)
  69.  
  70. data = "Ciao"
  71. dataLen = length(data)
  72.  
  73. down = 0
  74. do while 1
  75.  
  76.     n = SendTo(sock,data,0,"REMOTE")
  77.     if n!=dataLen then do
  78.         say "udp SendTo() error:" errno()
  79.         exit
  80.     end
  81.  
  82.     call delay(10)
  83.  
  84.     if down then call WriteCh("STDOUT",".")
  85.  
  86.     n = RecvFrom(sock,"BUFF",10,0,"REMOTE")
  87.     if n==-1 then do
  88.         err = errno()
  89.         if err==35 then do
  90.             if ~down then do
  91.                 call WriteCh("STDOUT","udp: no more data.")
  92.                 down = 1
  93.             end
  94.         end
  95.         else do
  96.             say "udp: RecvFrom() error:" err
  97.             exit
  98.         end
  99.     end
  100.     else do
  101.         say "udp: read byte(s):" n
  102.         down = 0
  103.     end
  104.  
  105. end
  106.